package com.ruijixiang.leetcode.hot100;

import java.util.Stack;

public class leetcode0020 {
    public boolean isValid(String s){
        Stack<Character> stack=new Stack<>();
        char[] chs=s.toCharArray();
        if(chs.length%2==1) return false;
        for(int i=0;i<chs.length;i++){
            if(chs[i]=='(' || chs[i]=='{' || chs[i]=='['){
                stack.push(chs[i]);
            }else{
                if(stack.isEmpty()) return false;
                if(chs[i]==')' && stack.peek()=='('){
                    stack.pop();
                }else if(chs[i]=='}' && stack.peek()=='{'){
                    stack.pop();
                }else if(chs[i]==']' && stack.peek()=='['){
                    stack.pop();
                }else{
                    return false;
                }
            }
        }
        return stack.isEmpty() ? true : false;
    }
}
